package com.gz;

//注:守护线程(这里没写)，是随主进程结束一起结束的进程
//用t1.setDaemon(true);来实现

public class Thread01 {
    public static void main(String[] args) throws InterruptedException {
        MyThread myThread = new MyThread();

        for (int i = 1; i < 11; i++) {
            Thread.sleep(1000);
            System.out.println("i "+i);
            if(i==5) {
                Thread t1 = new Thread(myThread);//如果是用extends Thread,就不需要这步,直接myThread.start();
                t1.start();
                t1.join();//启动后立即将子线程t1插入主线程
            }
        }
    }

}

class MyThread implements Runnable {
    private int count = 0;
    public void run(){
        while(true){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("hello "+count++);
            if(count==10)
                break;
        }
        System.out.println("子线程结束");
    }
}

